package com.example.a2209_mianshi.base

import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import com.example.a2209_mianshi.R
import com.example.a2209_mianshi.ui.mall.MallHomeMultiAdapter
import com.example.a2209_mianshi.ui.mall.model.MallHomeRowItem

/**
 * Created by ZengCS on 2022/8/17.
 * E-mail:zengcs@vip.qq.com
 * Add:中国成都
 * Desc: 包含下拉刷新和上拉加载更多的带Toolbar的Fragment
 *
 * @param VM ViewModel
 * @param VDB 页面的ViewDataBinding
 */
abstract class BaseMallHomeDataFragment<VM : BaseViewModel, VDB : ViewDataBinding> : BaseActivity<VM, VDB>() {
    var mAdapter: MallHomeMultiAdapter? = null
    var mItems = ArrayList<MallHomeRowItem>()
//    var smartRefreshLayout: SmartRefreshLayout? = null

//    var emptyTips = ErrorEmptyUIUtils.EMPTY_TIPS
//    var errorTips = ErrorEmptyUIUtils.ERROR_TIPS
    private val emptyView by lazy {
        layoutInflater.inflate(R.layout.view_empty_ui, null, false)
    }

//    override fun setLoadingViewVisible(show: Boolean, msg: String) {
//        super.setLoadingViewVisible(show, msg)
//        if (!show) this.smartRefreshLayout?.finishRefresh()
//    }

    abstract fun initAdapter()

    // 从网络获取数据
    open fun getDataFromNet(onRefresh: Boolean = false) {
        if (!onRefresh)
            setLoadingViewVisible(true, "数据加载中...")
    }

//    fun attachSmartRefreshLayout(smartRefreshLayout: SmartRefreshLayout) {
//        this.smartRefreshLayout = smartRefreshLayout
//        smartRefreshLayout.setOnRefreshListener {
//            onRefresh()
//        }
//    }
//
//    fun onLoadError() {
//        if (mItems.isNullOrEmpty()) {
//            mAdapter?.data?.clear()
//            mAdapter?.setEmptyView(emptyView)
//            mAdapter?.notifyDataSetChanged()
//        }
//    }

    // 展示数据
    open fun invalidateList(resultList: List<MallHomeRowItem>) {
//        ErrorEmptyUIUtils.updateEmptyUI(
//            emptyView = emptyView,
//            pages = pages,
//            errorTips = errorTips,
//            emptyTips = emptyTips,
//            onRefresh = {
//                onRefresh()
//            }
//        )

        mItems.clear()
        mItems.addAll(resultList)

        if (mAdapter == null) {
            initAdapter()
        } else {
            // mAdapter?.setNewInstance(mItems)
            mAdapter?.notifyDataSetChanged()
        }

        if (mItems.isNullOrEmpty()) {
            mAdapter?.data?.clear()
            mAdapter?.setEmptyView(emptyView)
            mAdapter?.notifyDataSetChanged()
        }
    }

    fun onRefresh() {
        if (mAdapter?.hasEmptyView() == true) {
            mAdapter?.removeEmptyView()
            mAdapter?.notifyDataSetChanged()
        }
        getDataFromNet(true)
    }

//    override fun onLoadFailed(msg: String) {
//        errorTips = msg
//        invalidateList(emptyList(), ErrorEmptyUIUtils.ERROR_PAGE_SIZE)
//    }
//
//    fun onLoadFailed(msg: String, retryAble: Boolean = false) {
//        if (retryAble)
//            errorTips = msg
//        else
//            emptyTips = msg
//        invalidateList(
//            emptyList(), if (retryAble) ErrorEmptyUIUtils.ERROR_PAGE_SIZE
//            else ErrorEmptyUIUtils.ERROR_PAGE_SIZE + 1
//        )
//    }

    fun bindAdapter(rv: RecyclerView) {
        rv.adapter = mAdapter
    }
}